﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace test1
{
    public partial class Form1 : Form
    {
        //file标签清零
        private void clearright()
        {
            for (int j = 0; j < 8; j++)
            {
                switch (j)
                {
                    case 0:
                        file1.Text = "节目文件名";
                        size1.Text = "文件大小";
                        fenliu1.Checked = false;
                        data1.Clear();
                        info1.Clear();
                        break;
                    case 1:
                        file2.Text = "节目文件名";
                        size2.Text = "文件大小";
                        fenliu2.Checked = false;
                        data2.Clear();
                        info2.Clear(); 
                        break;
                    case 2:
                        file3.Text = "节目文件名";
                        size3.Text = "文件大小";
                        fenliu3.Checked = false;
                        data3.Clear();
                        info3.Clear();
                        break;
                    case 3:
                        file4.Text = "节目文件名";
                        size4.Text = "文件大小";
                        fenliu4.Checked = false;
                        data4.Clear();
                        info4.Clear();
                        break;
                    case 4:
                        file5.Text = "节目文件名";
                        size5.Text = "文件大小";
                        fenliu5.Checked = false;
                        data5.Clear();
                        info5.Clear();
                        break;
                    case 5:
                        file6.Text = "节目文件名";
                        size6.Text = "文件大小";
                        fenliu6.Checked = false;
                        data6.Clear();
                        info6.Clear();
                        break;
                    case 6:
                        file7.Text = "节目文件名";
                        size7.Text = "文件大小";
                        fenliu7.Checked = false;
                        data7.Clear();
                        info7.Clear();
                        break;
                    case 7:
                        file8.Text = "节目文件名";
                        size8.Text = "文件大小";
                        fenliu8.Checked = false;
                        data8.Clear();
                        info8.Clear();
                        break;
                }
            }
        }
        
        //从shipinlist抓取数据到file标签
        private void left2right()
        {
            clearright();
            //把 电视节目列表 里的数据赋给右边文件栏对应项目,tag标记右边的对应序号
            for (int j = 0; j < shipinlist.Items.Count; j++)
            {
                switch (j)
                {
                    case 0:
                        file1.Text = shipinlist.Items[0].SubItems[0].Text;
                        size1.Text = shipinlist.Items[0].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                    case 1:
                        file2.Text = shipinlist.Items[1].SubItems[0].Text;
                        size2.Text = shipinlist.Items[1].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j; 
                        break;
                    case 2:
                        file3.Text = shipinlist.Items[2].SubItems[0].Text;
                        size3.Text = shipinlist.Items[2].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                    case 3:
                        file4.Text = shipinlist.Items[3].SubItems[0].Text;
                        size4.Text = shipinlist.Items[3].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                    case 4:
                        file5.Text = shipinlist.Items[4].SubItems[0].Text;
                        size5.Text = shipinlist.Items[4].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                    case 5:
                        file6.Text = shipinlist.Items[5].SubItems[0].Text;
                        size6.Text = shipinlist.Items[5].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                    case 6:
                        file7.Text = shipinlist.Items[6].SubItems[0].Text;
                        size7.Text = shipinlist.Items[6].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                    case 7:
                        file8.Text = shipinlist.Items[7].SubItems[0].Text;
                        size8.Text = shipinlist.Items[7].SubItems[1].Text;
                        shipinlist.Items[j].Tag = j;
                        break;
                }
            }
        }
        
        //检查工作目录是否已经设置
        private void 检查工作目录()
        {
            if (workDirTBx.Text == "") //如果没有设置工作目录，就先设置工作目录。
            {
                MessageBox.Show("请选择工作目录先！\n所谓工作目录，就是一会要把命名好的文件移入的那个目录，你可以把ftp的上传默认路径选到那，会比较方便！\n工作目录最好和你下载好的节目在同一个磁盘分区上，不然移动文件的时候可能会很慢。");
                folderBrowserDialog1.ShowDialog();                  //用以存储右边栏所有文件的绝对路径
                workDirTBx.Text = folderBrowserDialog1.SelectedPath;
            }
            if (workDirTBx.Text == "")
            {
                MessageBox.Show("这个框框弹出来好累的，你想累死我哟！⊙﹏⊙汗！");
                folderBrowserDialog1.ShowDialog();                  //用以存储右边栏所有文件的绝对路径
                workDirTBx.Text = folderBrowserDialog1.SelectedPath;
            }
            if (workDirTBx.Text == "")
            {
                MessageBox.Show("日！你丫为啥不选？赶快选啊，不然后面会出错滴！\n前两次我原谅你了，再一再二没有再三！\n\n给你最后一次机会！好好把握！ O(∩_∩)O");
                folderBrowserDialog1.ShowDialog();                  //用以存储右边栏所有文件的绝对路径
                workDirTBx.Text = folderBrowserDialog1.SelectedPath;
            } 
            if (workDirTBx.Text == "")
            {
                MessageBox.Show("不选拉倒，爷不跟你玩了！");
                this.Close();
            }
        }

        //为combobox装载 服务器列表 项目
        private void LoadServerlist()
        {
            string session = "服务器名称";

            Setting tmp = new Setting();
            ArrayList data = new ArrayList();
            tmp.readsession( session, data);
            this.ComboBox34.DataSource = data;

            Setting tmp2 = new Setting();
            ArrayList data2 = new ArrayList();
            tmp2.readsession(session, data2);
            this.ComboBox35.DataSource = data2;

            Setting tmp3 = new Setting();
            ArrayList data3 = new ArrayList();
            tmp3.readsession(session, data3);
            this.comboBox1.DataSource = data3;
        }

        //由一个文本text1查找其在session1中的index，并在session2中找出这个index对应的text2
        private void GetUploadDir(string text1, string session1, ArrayList text2, string session2)
        {
            ArrayList content1 = new ArrayList();
            ArrayList content2 = new ArrayList();
            Setting tmp = new Setting();
            tmp.readsession(session1, content1);
            tmp.readsession(session2, content2);
            int index = content1.IndexOf(text1);
            text2.Add(content2[index].ToString());
            return;
        }

        //load 分流服务器
        private void LoadFenliuServer()
        {
            StringBuilder 分流服务器 = new StringBuilder();
            Setting tmp = new Setting();
            int temp = GetPrivateProfileString("杂项", "分流服务器", "分流服务器读取错误！", 分流服务器, 255, tmp.SettingFileName);
            分流服务器TBx.Text = 分流服务器.ToString();
        }

        //load 工作目录
        private void LoadWorkDir()
        {
            StringBuilder workdir = new StringBuilder(2550);
            Setting tmp = new Setting();
            int temp = GetPrivateProfileString("杂项", "工作目录", "工作目录读取错误！", workdir, 2550, tmp.SettingFileName);
            workDirTBx.Text = workdir.ToString();
            return;
        }

        //保存工作目录到ini
        private void SetWorkDir()
        {
            String workdir = workDirTBx.Text;
            Setting tmp = new Setting();
            WritePrivateProfileString("杂项", "工作目录", workdir,tmp.SettingFileName);
            MessageBox.Show("成功保存工作目录到配置文件！", "提示：");
            return;
        }

        //load 节目list
        private void LoadJiemuList()
        {
            //只用来显示节目list，识别文件名的时候不能调用此void，因为jiemulist已经变成两组内容了。
            ArrayList Jiemulist = new ArrayList();
            Setting tmp = new Setting();
            string session = "节目名称";
            tmp.readsession(session, Jiemulist);

            ArrayList tezheng = new ArrayList();
            Setting tmp2 = new Setting();
            string session2 = "文件名特征";
            tmp2.readsession(session2, tezheng);

            for (int i = 0; i < Jiemulist.Count; i++)
            {
                Jiemulist[i] += " 特征:" + tezheng[i].ToString();
            }

            this.ComboBox36.DataSource = Jiemulist;
        }

        //智能识别节目名
        private string GetJiemuName(string Filename)
        {
            string session1 = "文件名特征";
            string session2 = "节目名称";
            string JiemuName = "null0";

            Setting tmp = new Setting();
            ArrayList tezheng = new ArrayList();
            tmp.readsession(session1, tezheng);
            
            Setting tmp2 = new Setting();
            ArrayList jiemuming = new ArrayList();
            tmp2.readsession(session2, jiemuming);
            for (int i = 0; i < tezheng.Count; i++)
            {
                if (Filename.Contains(tezheng[i].ToString()))
                {
                    JiemuName = jiemuming[i].ToString();
                    return JiemuName;
                }
            }
            JiemuName = "null";
            MessageBox.Show("特征库不全，未能识别文件。这将导致帖子标题生成出错。 " + Filename +" ！\n为保证以后不出现此错误，请添加该节目的特征值。", "错误！");
            return JiemuName;
            
        }

        //智能识别特征
        private string GetTezheng(string Filename)
        {
            string session1 = "文件名特征";
            string session2 = "节目名称";
            string JiemuName = "null0";

            Setting tmp = new Setting();
            ArrayList tezheng = new ArrayList();
            tmp.readsession(session1, tezheng);

            Setting tmp2 = new Setting();
            ArrayList jiemuming = new ArrayList();
            tmp2.readsession(session2, jiemuming);
            for (int i = 0; i < tezheng.Count; i++)
            {
                if (Filename.Contains(tezheng[i].ToString()))
                {
                    JiemuName = jiemuming[i].ToString();
                    return tezheng[i].ToString();
                }
            }
            JiemuName = "没找到特征";
            MessageBox.Show("特征库不全，未能识别文件 " + Filename + " ！\n为保证以后不出现此错误，请添加该节目的特征值。", "错误！");
            return JiemuName;

        }

        //生成标题
        private void ProduceTitle()
        {
            ListView data = new ListView();
            data.Sorting = SortOrder.Ascending;

            for (int i = 0; i < uploadlist1.Items.Count ; i++) 
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = uploadlist1.Items[i].SubItems[0].Text;
                item.SubItems.Add( uploadlist1.Items[i].SubItems[5].Text);
                data.Items.Add(item);
            }
            for (int i = 0; i < uploadlist2.Items.Count; i++) 
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = uploadlist2.Items[i].SubItems[0].Text;
                item.SubItems.Add( uploadlist2.Items[i].SubItems[5].Text);
                data.Items.Add(item);
            }

            StringBuilder title = new StringBuilder();
            title.Append("[PUB:综艺节目]");
            for (int i = 0; i < data.Items.Count;i++ )
            {
                if (i == 0)
                {
                    title.Append(data.Items[i].SubItems[0].Text);
                    title.Append(data.Items[i].SubItems[1].Text);
                }
                else
                {
                    title.Append("|");
                    if (data.Items[i].SubItems[0].Text != data.Items[i - 1].SubItems[0].Text)
                    {
                        title.Append(data.Items[i].SubItems[0].Text);
                    }
                    title.Append(data.Items[i].SubItems[1].Text);
                }
            }
            int titlelength = title.Length;
            label4.Text = title.ToString();
            TextBox1.Text = title.ToString();
            if (titlelength < 110)
            {
                label5.Text = "标题长度为：" + titlelength.ToString();
            }
            else if (titlelength < 121)
            {
                label5.Text = "标题已满，请重新开帖";
                label5.ForeColor = Color.Red;
            }
            else
            {
                label5.Text = "标题长度超限，无法发布。\n你太贪了，一个帖子PUB这么多！自己手工改去吧，我不管了。";
            }
        }

        //C＃申明INI文件的写操作函数WritePrivateProfileString（）：
        //参数说明：section：INI文件中的段落；key：INI文件中的关键字；val：INI文件中关键字的数值；filePath：INI文件的完整的路径和名称。 
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        //C＃申明INI文件的读操作函数GetPrivateProfileString（）：
        //参数说明：section：INI文件中的段落名称；key：INI文件中的关键字；def：无法读取时候时候的缺省数值；retVal：读取数值；size：数值的大小；filePath：INI文件的完整路径和名称。 
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
        

        public class Setting
        {

            //设置ini文件路径
            public string SettingFileName = @".\settings.ini";              //@"C:\Documents and Settings\Administrator\桌面\settings.ini"; 
            
            /*对INI文件进行写操作的时候，如果此时在INI文件中存在和
                  要写入的信息相同的段落名称和关键字，则将覆盖此INI信息。*/
            //写入服务器信息

            public void readsession(string session, ArrayList data)
            {
                StringBuilder tmp = new StringBuilder();
                int LineNumber = GetPrivateProfileString(session, "LineNum", "配置文件读取出错，请检查settings.ini文件是否存在！", tmp, 255, SettingFileName);
                int i = 0;
                int LineNum = new int();
                do
                {
                    if (tmp.ToString() == i.ToString())
                    {
                        LineNum = i;
                        break;
                    }
                    else
                    {
                        i++;
                    }
                }
                while (i < 100);
                ArrayList Lines = new ArrayList(LineNum);
                for (int temp = 0; temp < LineNum;temp++ )
                {
                    Lines.Add( "Line" + (temp+1).ToString());
                    StringBuilder content = new StringBuilder(255);
                    int something =  GetPrivateProfileString(session, Lines[temp].ToString(), "配置文件读取出错，请检查settings.ini文件是否存在！", content, 255, SettingFileName);
                    data.Add(content.ToString());
                }
                return;
            }
            public void writesession(string session, ArrayList data)
            {
                //WritePrivateProfileString( session, null, null, SettingFileName);
                int LineNum = data.Count;
                WritePrivateProfileString( session, "LineNum", LineNum.ToString(), SettingFileName);
                ArrayList Lines = new ArrayList(data.Count + 1);
                for (int temp = 0; temp < LineNum;temp++ )
                {
                    Lines.Add( "Line" + (temp+1).ToString());
                    StringBuilder content = new StringBuilder(255);
                    WritePrivateProfileString(session, Lines[temp].ToString(), data[temp].ToString(), SettingFileName);
                }
                return;
            }
            
            
            //写入配置文件 服务器名称和路径
            public void AddServer(string servername, string serverroute)
            {
                string session = "服务器名称";
                string session2 = "服务器上传路径";
                ArrayList servers = new ArrayList();
                ArrayList routes = new ArrayList();
                //读写server
                readsession(session, servers);
                servers.Add(servername);
                writesession(session, servers);

                //读写route
                readsession(session2, routes);
                routes.Add(serverroute);
                writesession(session2, routes);
            }
            public void DelServer(string servername)
            {
                string session1 = "服务器名称";
                string session2 = "服务器上传路径";
                ArrayList servers = new ArrayList();
                ArrayList routes = new ArrayList();
                StringBuilder templory = new StringBuilder();
                int temploryy = GetPrivateProfileString(session1, "LineNum", "???", templory, 255, SettingFileName);
                if (templory.ToString() == "0") MessageBox.Show("你真二！啥都没了你还删！\n你TM想玩死我啊！(⊙o⊙)？", "警告");
                //读
                readsession(session1, servers);
                readsession(session2, routes);
                //获取元素的位置，并删除之
                //检查服务器名称是否正确
                bool Yes = servers.Contains(servername);
                if (!Yes)
                {
                    MessageBox.Show("该服务器并不存在在设置列表中，无需删除！\n\n你丫逗我玩呢吧！", "警告");
                    return;
                }
                int index = servers.IndexOf(servername);
                servers.RemoveAt(index);
                routes.RemoveAt(index);
                string lastline = "Line" + (servers.Count + 1).ToString();
                WritePrivateProfileString(session1, lastline, null, SettingFileName);
                WritePrivateProfileString(session2, lastline, null, SettingFileName);
                //写
                writesession(session1, servers);
                writesession(session2, routes);
            }

            //删除配置文件中一个session中的一项
            public void DelAnItemOfSession(string session ,string item)
            {
                ArrayList data = new ArrayList();
                StringBuilder templory = new StringBuilder();
                int temploryy = GetPrivateProfileString(session, "LineNum", "???", templory, 255, SettingFileName);
                if (templory.ToString() == "0") MessageBox.Show("你真二！啥都没了你还删！\n你TM想玩死我啊！(⊙o⊙)？", "警告");
                //读
                readsession(session, data);
                //获取元素的位置，并删除之
                //检查服务器名称是否正确
                bool Yes = data.Contains(item);
                if (!Yes)
                {
                    MessageBox.Show("该 “"+ item + "” 并不存在在设置列表中，无需删除！\n\n你丫逗我玩呢吧！", "警告");
                    return;
                }
                int index = data.IndexOf(item);
                data.RemoveAt(index);
                string lastline = "Line" + (data.Count + 1).ToString();
                WritePrivateProfileString(session, lastline, null, SettingFileName);        //删除最后一行
                //写
                writesession(session, data);
            }
        }

        public Form1()
        {
            InitializeComponent();
            clearright();
            LoadServerlist();
            LoadFenliuServer();
            LoadWorkDir();
            LoadJiemuList();
            
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //文件=〉打开=〉弹出打开文件对话框
            OpenFileDialog1.ShowDialog();
        }

        private void OpenFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

            //赋值给 电视节目列表
            for (int i = 0; i < this.OpenFileDialog1.FileNames.Length; i++)
            {
                if (shipinlist.Items.Count > 7) break;      //限定listview最多8项
                //建立数组
                string[] lable = new string[8];
                string[] filedir = new string[8];
                string[] size = new string[8];
                string[] jiemuname = new string[8];

                //实例化fileinfo类名叫info，以获取文件信息
                FileInfo info = new FileInfo(this.OpenFileDialog1.FileNames[i]);
                //获取视频文件信息
                lable[i] = this.OpenFileDialog1.SafeFileNames[i].ToString();
                filedir[i] = info.FullName.ToString();
                //获取文件长度
                long sizetmp = info.Length;
                
                if (sizetmp < 1024)
                {
                    size[i] = sizetmp.ToString();
                }
                else if (sizetmp < 1024 * 1024)
                {
                    size[i] = String.Format("{0:###.##}KB", (float)sizetmp / 1024);
                }
                else if (sizetmp < 1024 * 1024 * 1024)
                {
                    size[i] = String.Format("{0:###.##}MB", (float)sizetmp / (1024 * 1024));
                }
                else
                {
                    size[i] = String.Format("{0:###.##}GB", (float)sizetmp / (1024 * 1024 * 1024));
                }
                //智能识别节目名
                jiemuname[i] = GetJiemuName(lable[i]);
                
                //输出数据到列表
                string[] ItemArray = new string[] { lable[i], size[i], filedir[i],jiemuname[i]};
                //实例化一个listviewitem项叫item，把列表转化成listview项
                ListViewItem item = new ListViewItem(ItemArray);
                //item.Tag = shipinlist.Items.Count;
                //添加项
                shipinlist.Items.Add(item);

            }
            this.left2right();

        }

        private void Button9_Click(object sender, EventArgs e)
        {
            this.shipinlist.Items.Clear();
            file1.Text = "file1";
            size1.Text = "文件大小";
            file2.Text = "file2";
            size2.Text = "文件大小";
            file3.Text = "file3";
            size3.Text = "文件大小";
            file4.Text = "file4";
            size4.Text = "文件大小";
            file5.Text = "file5";
            size5.Text = "文件大小";
            file6.Text = "file6";
            size6.Text = "文件大小";
            file7.Text = "file7";
            size7.Text = "文件大小";
            file8.Text = "file8";
            size8.Text = "文件大小";
        }

        

        private void 退出AltF4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void shipinlist_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (shipinlist.SelectedItems.Count <= 0)        //判断是否选中项目
                {
                    return;
                }

                //把选择的文件放入一个string array中 

                string[] files = new String[shipinlist.SelectedItems.Count];

                int i = 0;
                foreach (ListViewItem item in shipinlist.SelectedItems)
                {
                    files[i++] = item.Text.ToString();
                }

                //create a dataobject holding this array as a filedrop

                DataObject data = new DataObject(DataFormats.FileDrop, files);

                //also add the selection as textdata

                //data.SetData(DataFormats.StringFormat, files[0]);

                //Do DragDrop
                DoDragDrop(data, DragDropEffects.Copy);
            }
        }

        private void uploadlist2_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void uploadlist2_DragDrop(object sender, DragEventArgs e)
        {
            if (MessageBox.Show("是否重命名这些文件并移动到工作目录？", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                检查工作目录();
                //文件重命名
                try
                {
                    
                    string[] data = { data1.Text, data2.Text, data3.Text, data4.Text, data5.Text, data6.Text, data7.Text, data8.Text };
                    bool[] fenliu = { fenliu1.Checked, fenliu2.Checked, fenliu3.Checked, fenliu4.Checked, fenliu5.Checked, fenliu6.Checked, fenliu7.Checked, fenliu8.Checked};
                    string[] info = { info1.Text,info2.Text,info3.Text,info4.Text,info5.Text,info6.Text,info7.Text,info8.Text};
                    string[] sizelist2 = { size1.Text,size2.Text,size3.Text,size4.Text,size5.Text,size6.Text,size7.Text,size8.Text};
                    foreach (ListViewItem item in this.shipinlist.SelectedItems)
                    {
                        FileInfo file = new FileInfo(item.SubItems[2].Text);
                        //检查文件是否存在
                        if (file.Exists || workDirTBx.Text != "")
                        {
                            if (data[(int)item.Tag] == "  .")                   //检查是否输入了节目播出日期
                            {
                                MessageBox.Show("请输入节目 “" + file.Name +"” 的播出日期！");
                                return;
                            }
                            //获取替换特征值之后的新的文件名
                            string oldfilename = item.SubItems[0].Text;
                            string jiemuname = GetJiemuName(oldfilename);
                            string tezheng = GetTezheng(oldfilename);
                            string newname = oldfilename.Replace(tezheng, jiemuname);

                            //移动文件并重命名
                            string filename = workDirTBx.Text + "\\Ftp2\\" + data[(int)item.Tag] + "\\" + newname;             //定义新文件名
                            DirectoryInfo destdir = new DirectoryInfo(workDirTBx.Text + "\\Ftp2\\" + data[(int)item.Tag] + "\\");   //定义新目录
                            if (destdir.Exists == false) destdir.Create();
                            file.MoveTo(filename);              //如果文件移动跨分区，会很慢！

                            //infolist2[] 是存储 资源发布时每个文件一行的那个信息 的数组  如 “欢乐幸运星-2008-05-27-YDY.rmvb [主持人:胡瓜 来宾:小芸 杰夫 嘉伸] 155MB”
                            if (info[(int)item.Tag] != "")
                            {
                                info[(int)item.Tag] = "[" + info[(int)item.Tag] + "]";
                            }
                                                        
                            //移动列表项
                            //将所需信息写入到临时的listviewitem中
                            ListViewItem itemtmp = new ListViewItem();
                            itemtmp.SubItems[0].Text = data[(int)item.Tag];
                            itemtmp.SubItems.Add(item.Text);
                            itemtmp.SubItems.Add(filename);
                            itemtmp.SubItems.Add( file.Name + "     " + info[(int)item.Tag] + "     " + sizelist2[(int)item.Tag]);
                            itemtmp.SubItems.Add(fenliu[(int)item.Tag].ToString());
                            itemtmp.SubItems.Add(jiemuname);
                            //添加该listitem
                            this.uploadlist2.BeginUpdate();
                            this.uploadlist2.Items.Add(itemtmp);
                            this.shipinlist.Items.Remove(item);
                            this.uploadlist2.EndUpdate();

                            ProduceTitle();

                            continue;
                        }
                        else
                        {
                            string errormessage = "文件 “" + file.Name + "” 不存在，或者未设置工作目录！";
                            MessageBox.Show(errormessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }


                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                
            }



        }

        private void Button12_Click(object sender, EventArgs e)
        {
            //设置工作路径
            folderBrowserDialog1.ShowDialog();
            workDirTBx.Text = folderBrowserDialog1.SelectedPath;
            //此处要set dir 到ini文件中
            SetWorkDir();
        }

        private void TabControl1_Selected(object sender, TabControlEventArgs e)
        {
            RichTextBox1.Clear();
            //判断是否需要生成text
            if (uploadlist2.Items.Count < 1 && uploadlist1.Items.Count < 1) return;   //两个list里面都没有元素，返回。

            long allsize = new long();
            ArrayList fenliulist = new ArrayList();
            if (uploadlist1.Items.Count > 0)            //ftp1有元素则应该计算info，否则跳过
            {
                //判断是否需要输出路径行
                ArrayList ftp1 = new ArrayList();      //装载ftp1的info块的数组
                string tmp1 = uploadlist1.Items[0].Text;
                ftp1.Add("[color=green]" + Label14.Text + "\\[/color][color=red]" + uploadlist1.Items[0].Text + "\\[/color]" + "\n");    //路径
                foreach (ListViewItem item in uploadlist1.Items)
                {
                    if (string.Compare(tmp1, item.Text) != 0)
                        ftp1.Add("[color=green]" + Label14.Text + "\\[/color][color=red]" + item.Text + "\\[/color]" + "\n");      //路径
                    ftp1.Add(item.SubItems[3].Text + "\n");                             //简介行
                    FileInfo fileinfo = new FileInfo(item.SubItems[2].Text);
                    allsize += fileinfo.Length;
                    if (item.SubItems[4].Text == "True") fenliulist.Add(item.SubItems[1].Text + Label12.Text + 分流服务器TBx.Text + "\n");
                    if (item.Index == 0) continue; else tmp1 = item.Text;
                }
                ftp1.Insert(0, "\n[color=red][b]" + ComboBox34.SelectedItem.ToString() + "[/b][/color]\n");                          //ftp名称
                ftp1.Add("\n");                                                         //末尾换行

                //输出ftp1的info块
                foreach (object a in ftp1)
                {
                    RichTextBox1.Text += a.ToString();
                }
            }

            if (uploadlist2.Items.Count > 0)            //ftp2有元素则应该计算info，否则跳过
            {
                //判断是否需要输出路径行
                ArrayList ftp2 = new ArrayList();      //装载ftp2的info块的数组
                string tmp = uploadlist2.Items[0].Text;
                ftp2.Add("[color=green]" + Label15.Text + "\\[/color][color=red]" + uploadlist2.Items[0].Text + "\\[/color]" + "\n");    //路径
                foreach (ListViewItem item in uploadlist2.Items)
                {
                    if (string.Compare(tmp, item.Text) != 0)
                        ftp2.Add("[color=green]" + Label15.Text + "\\[/color][color=red]" + item.Text + "\\[/color]" + "\n");      //路径
                    ftp2.Add(item.SubItems[3].Text + "\n");                             //简介行
                    FileInfo fileinfo = new FileInfo(item.SubItems[2].Text);
                    allsize += fileinfo.Length; 
                    if (item.SubItems[4].Text == "True") fenliulist.Add(item.SubItems[1].Text + Label12.Text + 分流服务器TBx.Text + "\n");
                    if (item.Index == 0) continue; else tmp = item.Text;
                }
                ftp2.Insert(0, "\n[color=red][b]" + ComboBox35.SelectedItem.ToString() + "[/b][/color]\n");                          //ftp名称
                ftp2.Add("\n");                                                         //末尾换行

                //输出ftp2的info块
                foreach (object b in ftp2)
                {
                    RichTextBox1.Text += b.ToString();
                }
            }
            //有需要分流的文件，则输出分流列表
            if (fenliulist.Count > 0)
            {
                foreach (object fenliu in fenliulist)
                {
                    RichTextBox1.Text += fenliu.ToString();
                }
            }

            //输出总文件大小
            string finalsize = String.Format("\n[color=blue][b]All：{0:###.##}MB[/b][/color]", (float)allsize / (1024 * 1024));
            RichTextBox1.Text += finalsize;

            ProduceTitle();
            
        }

        private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
        {
            left2right();
        }

        private void 清空CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            clearright();
        }

        private void uploadlist1_DragDrop(object sender, DragEventArgs e)
        {

            if (MessageBox.Show("是否重命名这些文件并移动到工作目录？", "注意", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                检查工作目录();
                //文件重命名
                try
                {

                    string[] data = { data1.Text, data2.Text, data3.Text, data4.Text, data5.Text, data6.Text, data7.Text, data8.Text };
                    bool[] fenliu = { fenliu1.Checked, fenliu2.Checked, fenliu3.Checked, fenliu4.Checked, fenliu5.Checked, fenliu6.Checked, fenliu7.Checked, fenliu8.Checked };
                    string[] info = { info1.Text, info2.Text, info3.Text, info4.Text, info5.Text, info6.Text, info7.Text, info8.Text };
                    string[] sizelist2 = { size1.Text, size2.Text, size3.Text, size4.Text, size5.Text, size6.Text, size7.Text, size8.Text };
                    foreach (ListViewItem item in this.shipinlist.SelectedItems)
                    {
                        FileInfo file = new FileInfo(item.SubItems[2].Text);
                        if (file.Exists || workDirTBx.Text != "")
                        {
                            if (data[(int)item.Tag] == "  .")
                            {
                                MessageBox.Show("请输入节目 “" + file.Name + "” 的播出日期！");
                                return;
                            }

                            //获取替换特征值之后的新的文件名
                            string oldfilename = item.SubItems[0].Text;
                            string jiemuname = GetJiemuName(oldfilename);
                            string tezheng = GetTezheng(oldfilename);
                            string newname = oldfilename.Replace(tezheng, jiemuname);

                            string filename = workDirTBx.Text + "\\Ftp1\\" + data[(int)item.Tag] + "\\" + newname;             //定义新文件名
                            DirectoryInfo destdir = new DirectoryInfo(workDirTBx.Text + "\\Ftp1\\" + data[(int)item.Tag] + "\\");   //定义新目录
                            if (destdir.Exists == false) destdir.Create();
                            file.MoveTo(filename);              //如果文件移动跨分区，会很慢！

                            //infolist2[] 是存储 资源发布时每个文件一行的那个信息 的数组  如 “欢乐幸运星-2008-05-27-YDY.rmvb [主持人:胡瓜 来宾:小芸 杰夫 嘉伸] 155MB”
                            if (info[(int)item.Tag] != "") info[(int)item.Tag] = "[" + info[(int)item.Tag] + "]";
                            
                            //移动列表项
                            //将所需信息写入到临时的listviewitem中
                            ListViewItem itemtmp = new ListViewItem();
                            itemtmp.SubItems[0].Text = data[(int)item.Tag];
                            itemtmp.SubItems.Add(item.Text);
                            itemtmp.SubItems.Add(filename);
                            itemtmp.SubItems.Add(file.Name + "     " + info[(int)item.Tag] + "     " + sizelist2[(int)item.Tag]);
                            itemtmp.SubItems.Add(fenliu[(int)item.Tag].ToString());
                            itemtmp.SubItems.Add(jiemuname);
                            //添加该listitem
                            this.uploadlist1.BeginUpdate();
                            this.uploadlist1.Items.Add(itemtmp);
                            this.shipinlist.Items.Remove(item);
                            this.uploadlist1.EndUpdate();

                            ProduceTitle();

                            continue;
                        }
                        else
                        {
                            string errormessage = "文件 “" + file.Name + "” 不存在，或者未设置工作目录！";
                            MessageBox.Show(errormessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }


                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }

        private void uploadlist1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void button14_MouseClick(object sender, MouseEventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.UnicodeText, RichTextBox1.Text);
        }

        private void button13_MouseClick(object sender, MouseEventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Text, TextBox1.Text);
        }

        private void 全部清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //this.clearright();
            //this.shipinlist.Items.Clear();
            System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
            this.Close();
            
        }

        private void 设置SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabControl1.SelectedIndex = 2;
        }

        private void 帮助F1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabControl1.SelectedIndex = 3;
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabControl1.SelectedIndex = 3;
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //添加服务器和路径到ini文件中。
            Setting tmp = new Setting();
            string servername = textBox2.Text;
            string serverroute = textBox4.Text;
            bool yes = textBox2.Text.Length > 0 && textBox4.Text.Length > 0;
            if (!yes)
            {
                MessageBox.Show("这俩框都不能为空！\n回去给我充填！  O(∩_∩)O~", "警告");
                textBox2.Clear();
                textBox4.Clear();
                return;
            }
            tmp.AddServer(servername, serverroute);
            textBox2.Clear();
            textBox4.Clear();
            LoadServerlist();
            MessageBox.Show("该服务器已添加！");
        }

        private void ComboBox34_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string text1 = ComboBox34.SelectedItem.ToString();
            ArrayList text2 = new ArrayList();
            string session1 = "服务器名称";
            string session2 = "服务器上传路径";
            GetUploadDir(text1, session1,text2 , session2);
            Label14.Text = text2[0].ToString();
        }

        private void ComboBox35_SelectionChangeCommitted(object sender, EventArgs e)
        {
            string text1 = ComboBox35.SelectedItem.ToString();
            ArrayList text2 = new ArrayList();
            string session1 = "服务器名称";
            string session2 = "服务器上传路径";
            GetUploadDir(text1, session1, text2, session2);
            Label15.Text = text2[0].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //DelServerList
            if (comboBox1.Items.Count == 0)
            {
                MessageBox.Show("叫你不听话乱删！你看，出错了吧！\n给我添加服务器去！", "警告");
                comboBox1.ResetText();
                ComboBox34.ResetText();
                ComboBox35.ResetText();
                return;
            }
            string text = comboBox1.SelectedItem.ToString();
            if (text.Length < 1)
            {
                MessageBox.Show("你傻冒啊，不告诉我删谁选就点啊！", "警告！");
                return;
            }
            Setting tmp = new Setting();
            tmp.DelServer(text);
            LoadServerlist();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string 分流服务器 = 分流服务器TBx.Text;
            if (分流服务器.Length < 1)
            {
                MessageBox.Show("此处不得为空！");
                return;
            }
            Setting tmp = new Setting();
            WritePrivateProfileString("杂项", "分流服务器", 分流服务器, tmp.SettingFileName);
            MessageBox.Show("成功保存分流服务器名称到配置文件！", "提示：");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //AddJiemuList
            if (textBox5.Text.Length < 1 )
            {
                MessageBox.Show("要识别成啥得告诉我呀！", "警告！");
                return;
            }
            if (textBox6.Text.Length < 1)
            {
                MessageBox.Show("你要识别的玩意儿有啥特征啊？特征值是啥啊？", "警告！");
                return;
            }

            Setting tmp = new Setting();
            string session = "节目名称";
            ArrayList data = new ArrayList();
            tmp.readsession(session, data);
            data.Add(textBox5.Text);
            tmp.writesession(session, data);
            textBox5.Clear();

            Setting tmp2 = new Setting();
            string session2 = "文件名特征";
            ArrayList data2 = new ArrayList();
            tmp2.readsession(session2, data2);
            data2.Add(textBox6.Text);
            tmp2.writesession(session2, data2);
            textBox6.Clear();

            LoadJiemuList(); 
            MessageBox.Show("写入新关键词成功！", "恭喜：");
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //DelJiemuList 
            if (ComboBox36.Items.Count == 0) 
            {
                MessageBox.Show("叫你不听话乱删！你看，出错了吧！\n都让你删没了！给我添加节目去！", "警告");
                ComboBox36.ResetText();
                return;
            }
            string text = ComboBox36.SelectedItem.ToString();
            if (text.Length < 1)
            {
                MessageBox.Show("你傻冒啊，不告诉我删谁选就点啊！", "警告！");
                return;
            }
            Setting tmp = new Setting();
            tmp.DelAnItemOfSession("节目名称", text);
            LoadJiemuList();
            MessageBox.Show("该节目删除成功！", "恭喜：");
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) //Enter键值是13 
            {
                button1.PerformClick();
            }
        }

        private void 分流服务器TBx_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) //Enter键值是13 
            {
                button3.PerformClick();
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.bitunion.org/thread-10115585-1-1.html");
        }

       

    }
}